<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="
https://cdn.jsdelivr.net/npm/axios@1.10.0/dist/axios.min.js
"></script>
</head>
<body>
  <button id="btn">登陆</button>
  <button id="btn2">获取order信息</button>
  <button id="btn3">获取user信息</button>
  <script>
    document.getElementById('btn').addEventListener('click', () => {
      fetch('http://localhost:3000/order/login', {
        method: 'POST',
        credentials: 'include',
        body:{
          username: 'admin',
          password: '123456'
        }
      }).then(res => res.json()).then(data => {
        console.log(data)
        localStorage.setItem('token', data.data.token)
      })
    })
    document.getElementById('btn2').addEventListener('click', () => {
      // fetch('http://localhost:3000/order', {
      //   method: 'GET',
      //   credentials: 'include',
      // }).then(res => res.json()).then(data => {
      //   console.log(data)
      // })
      axios.get('http://localhost:3000/order', {
        withCredentials: true,
      }).then(res => {
        console.log(res)
      })
    })
    document.getElementById('btn3').addEventListener('click', () => {
     if(localStorage.getItem('token')){
      fetch('http://localhost:3000/user', {
        method: 'GET',
        credentials: 'include',
        headers: {
          Authorization: localStorage.getItem('token')
        }
      }).then(res => res.json()).then(data => {
        console.log(data)
      })
     }else{
      fetch('http://localhost:3000/user', {
        method: 'GET',
        credentials: 'include',
      }).then(res => res.json()).then(data => {
        console.log(data)
      })
     }
    })
  </script>
</body>
</html>